package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.RankingListAdapter;
import com.xgkj.diyiketang.data.RankingInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.RankingTask;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 李睿 on 2017/4/26.
 */

public class RankingActivity extends Activity implements View.OnClickListener,PullToRefreshListView.OnRefreshListener{

    private PullToRefreshListView ranking_list;
    private RelativeLayout videolist_back;
    private RankingListAdapter adapter;
    private List<RankingInfo> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.ranking_activity);
        initView();
        getRankingInfo();
    }
    private void initView(){
        videolist_back = (RelativeLayout) findViewById(R.id.videolist_back);
        ranking_list = (PullToRefreshListView)findViewById(R.id.ranking_list);
        videolist_back.setOnClickListener(this);

        ranking_list.init(RankingActivity.this,true);
        ranking_list.setOnRefreshListener(this);
        adapter = new RankingListAdapter(RankingActivity.this);
        ranking_list.setAdapter(adapter);
        ranking_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position>0&&position<=list.size()){
//                    RankingInfo info = (RankingInfo) adapter.getItem(position);
                    RankingInfo info = list.get(position-1);
                    String userId = info.getUserId();
                    Intent intent = new Intent(RankingActivity.this, HisDetailActivity.class);
                    intent.putExtra("USERID", userId);
                    startActivity(intent);
                }
            }
        });
    }

    public EatShowApp getEatShowApp(){
        return  (EatShowApp)RankingActivity.this.getApplication();
    }
    private int rankingTaskId = -1;
    private void getRankingInfo(){
        RankingTask rankingTask = new RankingTask();
        rankingTask.addListener(rankingListener);
        rankingTaskId = getEatShowApp().getTaskManager().addTask(rankingTask);
    }

    private int currentPage = 1;
    private TaskEventListener rankingListener = new ShowToastTaskEventListener(RankingActivity.this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == rankingTaskId){
                if (currentPage == 1) {
                    ranking_list.onRefreshComplete();
                    ranking_list.enableLoadMore();
                } else {
                    ranking_list.onLoadDataComplete(true);
                }
                if (data != null){
                    list = (List<RankingInfo>) data;
                    if (ranking_list != null && list.size() > 0) {
                        adapter.updateData(list, true);
                        adapter.notifyDataSetChanged();
                    }
                }


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.videolist_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getRankingInfo();
    }
}
